﻿using InteControlSys.VehicleControlService;
using InteControlSys.ViewModel;
using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using System.Xml.Linq;

namespace InteControlSys
{
    public class MainWindowViewModel : INPCBase
    {
        //public static CurrentOp CurrentOp = CurrentOp.None;
        public static VehicleControlClient ControlService = new VehicleControlClient();
        private NodeViewModel currentSelectedObject;//当前选中对象
        public NodeViewModel CurrentSelectedObject
        {
            get { return currentSelectedObject; }
            set
            {
                currentSelectedObject = value;
                NotifyChanged("CurrentSelectedObject");
            }
        }
        private ProcessViewModel processVM;//过程列表
        public ProcessViewModel ProcessVM
        {
            get { return processVM; }
            set
            {
                processVM = value;
                NotifyChanged("ProcessVM");
            }
        }
        private DelegateViewModel delegateVM;//委托列表
        public DelegateViewModel DelegateVM
        {
            get { return delegateVM; }
            set
            {
                delegateVM = value;
                NotifyChanged("DelegateVM");
            }
        }
        private SampleManageControlViewModel sampleManageControlViewModel;//主列表
        public SampleManageControlViewModel SampleManageControlViewModel
        {
            get { return sampleManageControlViewModel; }
            set
            {
                sampleManageControlViewModel = value;
                NotifyChanged("SampleManageControlViewModel");
            }
        }
        private TrainControlViewModel trainControlViewModel;
        public TrainControlViewModel TrainControlViewModel
        {
            get { return trainControlViewModel; }
            set
            {
                trainControlViewModel = value;
                NotifyChanged("TrainControlViewModel");
            }
        }       
        private bool isEditWindowVisible;
        public bool IsEditWindowVisible
        {
            get { return isEditWindowVisible; }
            set
            {
                isEditWindowVisible = value;
                this.NotifyChanged("IsEditWindowVisible");
            }
        }

        private bool isLoading;
        public bool IsLoading
        {
            get { return isLoading; }
            set
            {
                isLoading = value;
                this.NotifyChanged("IsLoading");
            }
        }

        private NodeViewModel activeItem; //当前被激活的条目
        public NodeViewModel ActiveItem
        {
            get { return activeItem; }
            set
            {
                activeItem = value;
                this.NotifyChanged("ActiveItem");
            }
        }		
        
        public MainWindowViewModel()
        {
            Init();

        }
        #region command
        private SimpleCommand confirmCommand { get; set; }
        public SimpleCommand ConfirmCommand
        {
            get { return confirmCommand ?? (confirmCommand = new SimpleCommand(ExecuteConfirmCommand)); }
        }
        private SimpleCommand cancelCommand { get; set; }
        public SimpleCommand CancelCommand
        {
            get { return cancelCommand ?? (cancelCommand = new SimpleCommand(ExecuteCancelCommand)); }
        }
        public virtual void ExecuteConfirmCommand(object parameter) 
        {
            MessageBox.Show("调用改车牌接口");
            IsEditWindowVisible = false;
        }
        public virtual void ExecuteCancelCommand(object parameter) 
        {
            IsEditWindowVisible = false;
        }
        #endregion

        private DispatcherTimer timer;

        private void RefreshVehicleList()
        {
            var list = ControlService.GetTransportSummary();

            NodeViewModel item;
            ProcessViewModel vm = new ProcessViewModel(this);

            foreach (var li in list)
            {
                switch (li.Id)
                {
                    case -1:
                        item = new NoRecordViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 0:
                        item = new NotArrivedViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 1:
                        item = new ArriveNodeViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 2:
                        item = new WaitForReceiveViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 3:
                        item = new WaitingForDriverReplyViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 4:
                        item = new WaitingForScrapAdmissionViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 5:
                        item = new ReadyToExitParkingLotViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 6:
                        item = new ReadyToEnterScrapYardViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 7:
                        item = new WaitingForAllocationViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 8:
                        item = new CallNodeViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 9:
                        item = new FirstCheckNodeViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 10:
                        item = new EnteringNodeViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 11:
                        item = new FirstWeighingNodeViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 12:
                        item = new GoFeildNodeViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 13:
                        item = new GoSampleNodeViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 14:
                        item = new SamplingNodeViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 15:
                        item = new SampledNodeViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 16:
                        item = new UnloadingNodeViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 17:
                        item = new UnloadedNodeViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 18:
                        item = new SecondWeighingNodeViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 19:
                        item = new LeavingNodeViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 20:
                        item = new FilingNodeViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    case 21:
                        item = new FiledNodeViewModel(li.Name, this, li.Count, li.Id);
                        vm.RootChildren.Add(item);
                        break;
                    default:
                        break;
                }
            }

            this.ProcessVM = vm;

            DelegateViewModel dvm = new DelegateViewModel(this);
            item = new CheckDelegateNodeViewModel("检质");
            dvm.RootChildren.Add(item);
            item = new ConbineBatNodeViewModel("组批");
            dvm.RootChildren.Add(item);
            this.DelegateVM = dvm;
        }
        private void Init()
        {
            //sampleManageControlViewModel = new ViewModel.SampleManageControlViewModel();
            //trainControlViewModel = new ViewModel.TrainControlViewModel();

            RefreshVehicleList();
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(300);
            timer.Tick += timer1_Tick;
            timer.Start();
                        
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timer.Stop();
            RefreshVehicleList();
            timer.Start();
        }
        //public static void UpdateTree1Count()
        //{
        //    TransportInfo[] arrivedinfos;
        //    try
        //    {
        //        //arrivedinfos = MainWindowViewModel.ControlService.ListArrivedVehicle();
                
        //    }
        //    catch (Exception e)
        //    {
        //        MessageBox.Show(e.Message);
        //        return;
        //    }

        //}


        #region NoRecord
        private UnknownVehicleInfo SelectedUnknown;



        #endregion
    }

   
}